import re
import time
import json
import hashlib

from scrapy.utils.project import get_project_settings


first_url_mapping = get_project_settings().get('FIRST_URL_MAPPING', None)


def get_url(name):
    return first_url_mapping[name]['url']


def get_cls(name):
    return first_url_mapping[name]['cls']


def get_unique(name):
    return first_url_mapping[name]['unique_key']


def get_table_name(spider_name, typ=None):
    if typ:
        return 'amac_{}_{}'.format(spider_name, typ)
    else:
        return 'amac_{}'.format(spider_name)


def get_item_number_name(name):
    return '{}_item_number'.format(name)


def handle_url(url, id):
    return url + '/{}.html'.format(id)


def handle_string(string):
    if string:
        if isinstance(string, list):
            return ''.join(string).replace(' ', '').replace('\n', '').replace('\r', '').replace('\t', '')
        return string.replace(' ', '').replace('\n', '').replace('\r', '').replace('\t', '')
    return string


# def handle_items(items):
#     # 将items中的所有值置为None，防止创建数据库时丢失字段
#     for field in items.fields:
#         items[field] = None
#     return items


def get_unit(string):
    return re.search(r'.*?(\(.*\))', handle_string(string)).group(1)


def get_date(t):
    return time.strftime('%Y-%m-%d', time.localtime(t))


def get_datetime(t):
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t))


def get_uniques(name, page_type=None):
    unique_key = get_unique(name)   # 字典或字符串
    uniques = []
    if isinstance(unique_key, dict):
        if page_type and unique_key.get(page_type, None):
            unique = unique_key[page_type]
            if isinstance(unique, str):
                uniques.append(unique)
            elif isinstance(unique, list):
                uniques = unique
    if isinstance(unique_key, str):
        uniques.append(unique_key)
    if isinstance(unique_key, list):
        uniques = unique_key
    return uniques


def get_md5(*args):
    o = hashlib.md5()
    for params in args:
        if isinstance(params, int):
            o.update(str(params).encode())
        if isinstance(params, dict):
            o.update(json.dumps(dict, ensure_ascii=False).encode())
        if isinstance(params, (list, tuple, set)):
            o.update(','.join(params).encode())
        if isinstance(params, str):
            o.update(params.encode())
    return o.hexdigest()

